import java.io.*;
import java.util.Scanner;

// 学生管理系统
public class StudentManager {

    // 创建一个自定义的容器对象
    private static MyArray myArray = new MyArray();

    private static String fileName = "student.db";

    /**
     * sout: 快速输出sout
     * ctrl+x ：删除一行
     * shift+alt+上下键
     * shift+回车 光标回到下一行
     *
     * @param args
     */
    public static void main(String args[]) {

        // 1.创建一个扫描器
        Scanner scanner = new Scanner(System.in);

        // 加载文件中的数据
        loadData();

        // 2.写一个循环
        while (true) {
            System.out.println("1:添加学生，2：查询学生、3：删除学生、4：修改、5：持久化、6:机器人、7：退出");
            // 3.接收键盘输入
            int i = scanner.nextInt();
            if (i == 1) {

                System.out.println("请输入学生姓名:");
                String name = scanner.next();

                System.out.println("请输入学生的学号");
                int no = scanner.nextInt();

                System.out.println("请输入学生年龄");
                int age = scanner.nextInt();

                System.out.println("请输入学生的手机号");
                String mobile = scanner.next();

                System.out.println("请输入学生的性别");
                String sex = scanner.next();

                // 把用户输入的学生信息给封装到一个对象中
                Student stu = new Student(no, name, age, mobile, sex);

                // 把学生添加到集合中
                myArray.add(stu);

                System.out.println("【" + name + "】添加学生成功"); //sout
            } else if (i == 2) {
                Object[] students = myArray.getDate();
                for (Object student : students) {
                    System.out.println(student);
                }
            } else if (i == 3) {
                System.out.println("请输入要删除的学生学号");
                int no = scanner.nextInt();

                Object[] stuArray = myArray.getDate();
                boolean isDeleteFlag = false; // 来记录是否删除成功
                for (int j = 0; j < stuArray.length; j++) {
                    Student student = (Student) stuArray[j];
                    if (student.getNo() == no) {
                        myArray.remove(j); // 删除学生
                        isDeleteFlag = true; // 修改标志为删除成功
                        System.out.println("学生【" + student.getName() + "】删除成功");
                        break;
                    }
                }
                // 判断是否删除成功
                if (!isDeleteFlag) {
                    System.out.println("学生【" + no + "】不存在");
                }
            } else if (i == 4) {
                System.out.println("请输入要修改学生的学号");
                int no = scanner.nextInt();

                // 根据学号找到学生对象
                Student student = getStudentByNo(no);

                if (student == null) {
                    System.out.println("学生【" + no + "】不存在");
                    continue;
                }
                boolean reUpdate = true;
                while (reUpdate) {
                    System.out.println("输入你要修改学生的属性");
                    String att = scanner.next();

                    System.out.println("输入你要修改学生的属性值");
                    String val = scanner.next();

                    // 判断用户修改的是那个属性
                    if ("name".equals(att)) {
                        student.setName(val);
                    } else if ("age".equals(att)) {
                        student.setAge(Integer.parseInt(val));
                    } else if ("mobile".equals(att)) {
                        student.setMobile(val);
                    }

                    System.out.println("是否继续修改(y/n)?");
                    String isUpdate = scanner.next();
                    if ("n".equals(isUpdate)) {
                        System.out.println("修改结束");
                        reUpdate = false;
                    }
                }
            } else if (i == 5) {
                // 把数组给保存到文件中，而文件是保存在硬盘中的，从而做到了持久化。
                saveToFile();
            } else if (i == 6) {
                saveToFile();// 自动落盘
                System.out.println("bye bye ...");
                System.exit(-1);
            }
        }
    }

    private static void saveToFile() {
        FileOutputStream ops = null;
        ObjectOutputStream write = null;
        try {
            ops = new FileOutputStream("./" + fileName);
            write = new ObjectOutputStream(ops);
            write.writeObject(myArray);
            System.out.println("数据落盘成功");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            if (write != null) {
                try {
                    write.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (ops != null) {
                try {
                    ops.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    private static void loadData() {
        FileInputStream ips = null;
        ObjectInputStream reader = null;
        try {
            ips = new FileInputStream("./" + fileName);
            reader = new ObjectInputStream(ips);
            myArray = (MyArray) reader.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (ips != null) {
                try {
                    ips.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    // 根据学号返回学生对象
    private static Student getStudentByNo(int no) {
        for (Object o : myArray.getDate()) {
            Student stu = (Student) o;
            if (stu.getNo() == no) { // 对比学号了
                return stu;
            }
        }
        return null;
    }
}
